package io.github.betacat.sinluo.config;

import org.springframework.stereotype.Component;
import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

@Component
public class ExecBot extends TelegramLongPollingBot {

    //填你自己的token和username
    private String token = "7320606144:AAFYzNkFi_woCtABi-osiMf6RwEaJ0uKaBU";
    private String username = "sinluo_bot";

    public ExecBot() {
        this(createBotOptions());
    }

    private static DefaultBotOptions createBotOptions() {
        // 梯子在自己电脑上就写127.0.0.1 软路由就写路由器的地址
        String proxyHost = "127.0.0.1";
        // 端口根据实际情况填写，说明在上面，自己看
        int proxyPort = 7890;

        DefaultBotOptions botOptions = new DefaultBotOptions();
        botOptions.setProxyHost(proxyHost);
        botOptions.setProxyPort(proxyPort);
        // 注意一下这里，ProxyType是个枚举，看源码你就知道有NO_PROXY,HTTP,SOCKS4,SOCKS5;
        botOptions.setProxyType(DefaultBotOptions.ProxyType.SOCKS5);

        return botOptions;
    }

    public ExecBot(DefaultBotOptions options) {
        super(options);
    }

    @Override
    public String getBotToken() {
        return this.token;
    }

    @Override
    public String getBotUsername() {
        return this.username;
    }

    @Override
    public void onUpdateReceived(Update update) {
        if (update.hasMessage()) {
            Message message = update.getMessage();
            Long chatId = message.getChatId();
            String text = message.getText();
            sendMsg(text, chatId);
        }
    }


    //回复普通文本消息
    public void sendMsg(String text, Long chatId) {
        SendMessage response = new SendMessage();
        response.setChatId(String.valueOf(chatId));
        response.setText(text);
        try {
            execute(response);
        } catch (TelegramApiException e) {
        }
    }

}